package com.i2silly.wxbot.controller;

import com.alibaba.fastjson.JSON;
import com.i2silly.wxbot.constant.WxRobotConstant;
import com.i2silly.wxbot.pojo.BaseSend;
import com.i2silly.wxbot.pojo.MessageResp;
import com.i2silly.wxbot.pojo.MessageSend;
import com.i2silly.wxbot.pojo.ReceiverDTO;
import com.i2silly.wxbot.server.RobotServer;
import com.i2silly.wxbot.service.IBotContactService;
import com.i2silly.wxbot.utils.MessageUtil;
import com.i2silly.wxbot.utils.PubUtils;
import com.i2silly.wxbot.utils.ReplyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


/**
 * @program: wx-bot
 * @description：微信机器人控制类
 * @author: i2silly
 * @date: 2020年11月21日 15:00:55
 */
@Controller
@RequestMapping("/wx")
public class WxRobotController {

//    @Autowired
//    private RobotServer server;

    @Autowired
    private IBotContactService contactService;

    @RequestMapping("/getContact")
    @ResponseBody
    public String getContactList() {
//        server.getContactList();
        MessageResp resp = new MessageResp();
        resp.setData(contactService.list());
        return MessageUtil.respSuccess(resp);
    }

    @RequestMapping("/sendAtMsg")
    public void sendAtMsg() {
//        server.sendChatroomMsg(new MessageSend());
    }

    @RequestMapping("/sendTxtMsg")
    @ResponseBody
    public String sendTxtMsg(ReceiverDTO receive) {
        return null;
//        return MessageUtil.respSuccess(robotService.sendTxtMsg(receive));
    }

    @RequestMapping("/test")
    public void testSend() {
        BaseSend data = new BaseSend();
        DateTimeFormatter dtf =  DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        data.setId(PubUtils.getTime(System.currentTimeMillis(), dtf));
        data.setType(WxRobotConstant.GET_CHATROOM_MEMBER_TYPE);
        data.setWxid("null");
        data.setContent("5142327073@chatroom");
//        server.send(data);

        String function = ReplyUtil.getFunction();
//        server.sendMessage("muou0360", function);
        System.out.println("功能：" + function);
    }

    @RequestMapping("/getChatroom")
    public void testListChatroom() {
        BaseSend data = new BaseSend();
        DateTimeFormatter dtf =  DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        data.setId(PubUtils.getTime(System.currentTimeMillis(), dtf));
        data.setType(WxRobotConstant.GET_CHATROOM_TYPE);
        data.setWxid("null");
        data.setContent("roomId");
//        server.send(data);
    }
    @RequestMapping("/getMember")
    public void testGetChatroomMember(String s){
        BaseSend data = new BaseSend();
        DateTimeFormatter dtf =  DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        data.setId(PubUtils.getTime(System.currentTimeMillis(), dtf));
        data.setType(WxRobotConstant.GET_CHATROOM_MEMBER_TYPE);
        data.setWxid("null");
        data.setContent(s);
//        server.send(data);
    }

    @RequestMapping("/getFound")
    public void testGetFound() {
        String s = PubUtils.doGet(WxRobotConstant.FOUND_DETAIL_URL, null);
        List<Map> maps = JSON.parseArray(s, Map.class);
        System.out.println("数量：" + maps.size());
        System.out.println(maps);
    }

    @RequestMapping("/getTestFound")
    public void testFound() {
        String s = PubUtils.doGet(WxRobotConstant.TT_FOUND_URL + "320007.js", null);
        System.out.println(s);
        //jsonpgz({"fundcode":"320007","name":"诺安成长混合","jzrq":"2020-12-02","dwjz":"1.7900","gsz":"1.7611","gszzl":"-1.61","gztime":"2020-12-03 15:00"});
    }

    public static void main(String[] args) {
        String s = "jsonpgz({\"fundcode\":\"320007\",\"name\":\"诺安成长混合\",\"jzrq\":\"2020-12-02\",\"dwjz\":\"1.7900\",\"gsz\":\"1.7611\",\"gszzl\":\"-1.61\",\"gztime\":\"2020-12-03 15:00\"});";
        System.out.println(s);
        String s1 = s.replaceAll("jsonpgz\\(", "");
        System.out.println(s1.substring(0,s1.length() -2));
    }
}
